import React from "react";
import { Icon, IconProps } from ".";

const loadedSet = new Set<string>()
export function createFromIconfont(scriptUrl:string) {
    if (typeof scriptUrl === 'string' && scriptUrl.length && !loadedSet.has(scriptUrl)) {
        const script = document.createElement('script')
        script.setAttribute('src', scriptUrl)
        script.setAttribute('data-namespace', scriptUrl)
        document.body.appendChild(script)
        loadedSet.add(scriptUrl)
    }

    const Iconfont = React.forwardRef<SVGSVGElement, IconProps>((props, ref) => {
        const {type, ...rest} = props
        return (
            <Icon {...rest}>
                {type ? <use xlinkHref={`#${type}`}/> : null}
            </Icon>
        )
    })
    return Iconfont
}

const Iconfont = createFromIconfont('//at.alicdn.com/t/c/font_1330663_gdilv197rc6.js')
export function TestCreateFromIconfont(props: IconProps) {
    return <Iconfont {...props} />
}